package com.zdzx.controller.web;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdzx.common.result.Result;
import com.zdzx.common.result.ResultCode;
import com.zdzx.common.result.ResultUtil;
import com.zdzx.mapper.TenantMapper;
import com.zdzx.mapper.UserMapper;
import com.zdzx.model.service.base.result.TenantInfo;
import com.zdzx.model.service.base.vo.Tenant;
import com.zdzx.model.service.base.vo.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "web企业")
@RestController()
@RequestMapping("/web")
public class WebTenantController {

	
	@Autowired
	private TenantMapper tenantMapper;
	@Autowired
	private UserMapper userMapper;

	@ApiOperation(value = "租户详情", notes = "")
	@GetMapping("/enterprise/{enterpriseId}")
	public Result<TenantInfo> tenantDetails(@PathVariable("enterpriseId") String id) {
		TenantInfo info=new TenantInfo();
		QueryWrapper<Tenant> queryWrappere=new QueryWrapper<Tenant>();
		queryWrappere.eq("tenant_id", id);
		List<Tenant> tenants = tenantMapper.selectList(queryWrappere);
		if(tenants!=null&&tenants.size()>0) {
			Tenant tenant=tenants.get(0);
			BeanUtils.copyProperties(tenant, info);
			QueryWrapper<User> queryWrappers=new QueryWrapper<User>();
			queryWrappers.eq("role_type", 0);
			queryWrappers.eq("tenant_id", id);
			Integer sCount = userMapper.selectCount(queryWrappers);
			QueryWrapper<User> queryWrappert=new QueryWrapper<User>();
			queryWrappert.eq("role_type", 1);
			queryWrappert.eq("tenant_id", id);
			Integer tCount = userMapper.selectCount(queryWrappert);
			info.setTeacherCount(tCount==null?0:tCount);
			info.setStudentCount(sCount==null?0:sCount);
		}
		if(StringUtils.isEmpty(info)) {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), null, info);
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, info);
	}
}
